package com.examinantsdk.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.ToastUtils;
import com.examinantsdk.R;
import com.examinantsdk.base.view.IBaseView;
import com.examinantsdk.base.view.stateview.StateView;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;


/**
 * Create by Carson on 2021/3/17.
 * 父级Fragment
 */
public abstract class BaseFragment extends Fragment implements IBaseView {
    private StateView mStateView;
    private boolean dataLoaded = false;
    protected View rootView;
    private static final int NON_CODE = -1;
    protected Context mContext;
    public String TAG;
    protected boolean isVisible;
    public View toolBar;

    protected boolean isViewInitiated; //控件是否初始化完成
    protected boolean isVisibleToUser; //页面是否可见
    protected boolean isDataInitiated; //数据是否加载

    protected abstract int getContentViewLayout();

    protected abstract void initViewsAndEvents();

    /*
     * 是否绑定eventbus事件
     * */
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
    }

    @Override
    public void onResume() {
        super.onResume();
        this.isVisibleToUser = true;
        prepareFetchData();
    }

    public void lazyData(){}

    protected void prepareFetchData() {
        if (isVisibleToUser && isViewInitiated && !isDataInitiated) {
            lazyData();
            isDataInitiated = true;
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isBindEventBusHere()) {
            EventBus.getDefault().register(this);
        }
        TAG = this.getClass().getSimpleName();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(getContentViewLayout(), container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toolBar = view.findViewById(R.id.toolBar);
        initImmersionBar(toolBar);
        try {
            initViewsAndEvents();
            View viewTarget = bindStateTarget();
            if (viewTarget != null) {
                mStateView = StateView.inject(viewTarget);
//                mStateView.setTheme(isDarkTheme());
                mStateView.setStyle(R.drawable.bg_main_18dp);
                mStateView.setOnButtonClickListen(false, viewType -> {
                    if (viewType == StateView.RETRY) {
                        onErrorClick();
                    } else if (viewType == StateView.EMPTY) {
                        onEmptyClick();
                    }
                });
                showLoading();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        isViewInitiated = true;
        prepareFetchData();
    }

    /**
     * 绑定状态视图
     * @return
     */
    protected View bindStateTarget() {
        return null;
    }
    protected void onEmptyClick() { }
    protected void onErrorClick() { }

    @Override
    public void showEmpty(String msg) {
        if (mStateView != null) {
            mStateView.showEmpty(msg);
            dataLoaded = true;
        }
    }

    @Override
    public void showError(String msg) {
        if (mStateView != null && !dataLoaded) {
            mStateView.showRetry();
        }
    }

    @Override
    public void showLoading() {
        if (mStateView != null) {
            mStateView.showLoading();
        }
    }

    @Override
    public void showContent() {
        if (mStateView != null) {
            mStateView.showContent();
            dataLoaded = true;
        }
    }

    public void initImmersionBar(View toolBar) {
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .navigationBarColor(R.color.white)
                .navigationBarDarkIcon(true)
                .titleBar(this.toolBar).init();
    }

    //Toast居中提示
    public void showToast(String msg) {
        if (null != msg) {
            ToastUtils.showShort(msg);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (isBindEventBusHere()) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private long exitTime;

    /*
     * 屏蔽快速点击事件
     * */
    private boolean fastClick() {
        //屏蔽掉快速点击导致的多次打开新界面
        if (System.currentTimeMillis() - exitTime > 1500) {
            exitTime = System.currentTimeMillis();
        } else {
            return true;
        }
        return false;
    }

}
